package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CourseDao;
import com.itheima.domain.Course;
import com.itheima.service.CourseService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class CourseServiceImpl implements CourseService {
    @Override
    public PageInfo<Course> findByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Course> info = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CourseDao courseDao = MybatisUtil.getMapper(sqlSession, CourseDao.class);
            PageHelper.startPage(currentPage,pageSize);
            List<Course> list=courseDao.findAll();
            info = new PageInfo<>(list);
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return info;
    }

    @Override
    public void save(Course course) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CourseDao courseDao = MybatisUtil.getMapper(sqlSession, CourseDao.class);
            String id = UUID.randomUUID().toString();
            course.setId(id);
            courseDao.save(course);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public Course findById(String id) {
        SqlSession sqlSession = null;
        Course course= null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CourseDao courseDao = MybatisUtil.getMapper(sqlSession, CourseDao.class);
            course = courseDao.findById(id);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return course;
    }

    @Override
    public void update(Course course) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CourseDao courseDao = MybatisUtil.getMapper(sqlSession, CourseDao.class);

            courseDao.update(course);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CourseDao courseDao = MybatisUtil.getMapper(sqlSession, CourseDao.class);
            Stream.of(ids).forEach(id->courseDao.delete(id));
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Course> findAll() {
        SqlSession sqlSession = null;
        List<Course> list= null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CourseDao courseDao = MybatisUtil.getMapper(sqlSession, CourseDao.class);
            list = courseDao.findAll();
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }
}
